package com.example.yin.event;

import com.example.yin.dao.ArticlelikeMapper;
import com.example.yin.domain.Article;
import com.example.yin.domain.Articlelike;
import com.example.yin.domain.Song;
import com.example.yin.service.ArticleService;
import com.example.yin.service.SongService;
import com.example.yin.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Set;

@Component
@EnableScheduling
public class RedisToDbTask {

    private static long hit;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SongService songService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticlelikeMapper articlelikeMapper;



    @Scheduled(fixedRate = 30000) // 每30秒执行一次
    public void transferHittoDb() {
        Set<String> keys = redisTemplate.keys("song:*");
        Song song = new Song();
        for (String key : keys) {
            //通过key截取id，查询数据库中的hit，如果一样说明这段时间内没有人点击这首歌曲，从redis删除该键值对
            //否则更新数据库
            Integer id = Integer.parseInt(key.substring(5));
            Integer oldvalue = songService.searchHit(id);
            Integer value = redisCache.getCacheObject(key);

            if(oldvalue.equals(value)){
                redisCache.deleteObject(key);
            }
            song.setHit(value);
            song.setId(id);
            System.out.println(song);
            songService.updateSongMsg(song);
        }
    }

    @Scheduled(fixedRate = 30000)
    public void transferReadtoDb(){
        Set<String> keys = redisTemplate.keys("Article:*");
        Article article = new Article();
        for (String key : keys) {
            //通过key截取id，查询数据库中的hit，如果一样说明这段时间内没有人点击这首歌曲，从redis删除该键值对
            //否则更新数据库
            Integer id = Integer.parseInt(key.substring(8));
            Integer oldvalue = articleService.getDetail(id).getRead_count();
            Integer value = redisCache.getCacheObject(key);

            if(oldvalue.equals(value)){
                redisCache.deleteObject(key);
            }
            article.setRead_count(value);
            article.setArticle_id(id);
            //System.out.println(article);
            articleService.update(article);
        }
    }

    @Scheduled(fixedRate = 30000)
    public void transferliketoDb(){
        Set<String> keys = redisTemplate.keys("like*");
        Article article = new Article();
        for (String key : keys) {
            //通过key截取id，查询数据库中的hit，如果一样说明这段时间内没有人点击这首歌曲，从redis删除该键值对
            //否则更新数据库
            Integer id = Integer.parseInt(key.substring(4));
            Integer oldvalue = articleService.getDetail(id).getLike_count();
            Integer value = redisCache.getCacheObject(key);

            if(oldvalue.equals(value)){
                redisCache.deleteObject(key);
            }
            article.setRead_count(value);
            article.setArticle_id(id);
            //System.out.println(article);
            articleService.update(article);
        }
    }

    @Scheduled(fixedRate = 20000)
    public void transferArticleliketoDb(){
        Set<String> keys = redisTemplate.keys("Articlelike*");
        Articlelike articlelike = new Articlelike();
        for(String key : keys){
            Integer value = redisCache.getCacheObject(key);
            key = key.substring(11);
            String[] temp = key.split(":") ;

            Integer userid = Integer.parseInt(temp[0]);
            Integer articleId = Integer.parseInt(temp[1]);
            articlelike.setConsumerId(userid);
            articlelike.setUpdateTime(new Date());
            articlelike.setArticleId(articleId);
            if(value.equals(0)){
                articlelike.setIslike(null);
            }
            else{
                articlelike.setIslike(1);
            }
            articlelike.setArticleId(articleId);
            //System.out.println(userid+" "+articleId+" "+value);
            articlelikeMapper.insertSelective(articlelike);

        }
    }
}
